package com.block.api.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.block.common.bean.CommonParam;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.im.service.IMsgHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.Period;
import java.util.Date;

@RestController
@RequestMapping("api/admin/im")
@Validated
public class AdminImController {

    @Autowired
    IMsgHandleService msgHandleService;

    /**
     * 查询会话列表
     *
     */
    @RequestMapping(value = "/loginIn", method = {RequestMethod.POST, RequestMethod.GET})
    public String loginIn(@RequestAttribute(value = "tranData") JSONObject tranData,
                                 @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                 @RequestAttribute(value = "uri") String uri,
                                 @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                 @RequestAttribute(value = "startDate") Date startdate,
                                 @RequestAttribute(value = "ip") String ip) throws ApiException {

        // 管理后台用户ID
        String currUserId = tranData.getString("currUserId");
        String token = tranData.getString("token");
        boolean flushFlag = tranData.getBoolean("flushFlag");

        ResponseVo responseVo = msgHandleService.loginIn(currUserId, flushFlag);

        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }


    /**
     * 打招呼
     *
     */
    @RequestMapping(value = "/toSayHello", method = {RequestMethod.POST, RequestMethod.GET})
    public String toSayHello(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        // 管理后台用户ID
        String loginUid = tranData.getString("loginUid");
        String toUserId = tranData.getString("toUserId");
        String msgContent = tranData.getString("msgContent");

        ResponseVo responseVo = msgHandleService.toSayHello(loginUid, toUserId, msgContent);

        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

//    /**
//     * 查询会话列表
//     *
//     */
//    @RequestMapping(value = "/getSessionList", method = {RequestMethod.POST, RequestMethod.GET})
//    public String getSessionList(@RequestAttribute(value = "tranData") JSONObject tranData,
//                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
//                             @RequestAttribute(value = "uri") String uri,
//                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
//                             @RequestAttribute(value = "startDate") Date startdate,
//                             @RequestAttribute(value = "ip") String ip) throws ApiException {
//
//        // 管理后台用户ID
//        String currUserId = tranData.getString("currUserId");
//        Integer startIndex = tranData.getInteger("startIndex");
//
//        ResponseVo responseVo = msgHandleService.getSessionList(currUserId, startIndex);
//
//        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
//    }
//
//    /**
//     * 拉取单聊历史
//     *
//     */
//    @RequestMapping(value = "/getHistoryMsg", method = {RequestMethod.POST, RequestMethod.GET})
//    public String getHistoryMsg(@RequestAttribute(value = "tranData") JSONObject tranData,
//                               @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
//                               @RequestAttribute(value = "uri") String uri,
//                               @RequestAttribute(value = "commonParam") CommonParam commonParam,
//                               @RequestAttribute(value = "startDate") Date startdate,
//                               @RequestAttribute(value = "ip") String ip) throws ApiException {
//
//        String currUserId = tranData.getString("currUserId");
//        String peerImUserId = tranData.getString("peerImUserId");
//        String lastMsgKey = tranData.getString("lastMsgKey");
//
//        ResponseVo responseVo = msgHandleService.getHistoryMsg(currUserId, peerImUserId, lastMsgKey);
//
//        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
//    }
//
//    @RequestMapping(value = "/sendMsg", method = {RequestMethod.POST, RequestMethod.GET})
//    public String sendMsg(@RequestAttribute(value = "tranData") JSONObject tranData,
//                                @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
//                                @RequestAttribute(value = "uri") String uri,
//                                @RequestAttribute(value = "commonParam") CommonParam commonParam,
//                                @RequestAttribute(value = "startDate") Date startdate,
//                                @RequestAttribute(value = "ip") String ip) throws ApiException {
//
//        String fromAccountId = tranData.getString("currUserId");
//        String toImUserId = tranData.getString("toImUserId");
//        String msgContent = tranData.getString("msgContent");
//
//        ResponseVo responseVo = msgHandleService.sendMsg(fromAccountId, toImUserId, msgContent);
//
//        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
//    }

}
